package com.sunwayworld.basemodule.rm.train.train.service.impl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.sunwayworld.framework.at.annotation.AuditTrailType;
import com.sunwayworld.framework.at.annotation.AuditTrailEntry;
import com.sunwayworld.framework.spring.annotation.GikamBean;

import com.sunwayworld.basemodule.rm.train.train.dao.LimsTrainEffectDao;
import com.sunwayworld.basemodule.rm.train.train.bean.LimsTrainEffectBean;
import com.sunwayworld.basemodule.rm.train.train.service.LimsTrainEffectService;
import com.sunwayworld.framework.context.ApplicationContextHelper;
import com.sunwayworld.framework.restful.data.RestJsonWrapperBean;

import java.util.List;

@Repository
@GikamBean
public class LimsTrainEffectServiceImpl implements LimsTrainEffectService {

    @Autowired
    private LimsTrainEffectDao limsTrainEffectDao;

    @Override
    @SuppressWarnings("unchecked")
    public LimsTrainEffectDao getDao() {
        return limsTrainEffectDao;
    }

    @Override
    @Transactional
    @AuditTrailEntry(AuditTrailType.INSERT)
    public Long insert(RestJsonWrapperBean jsonWrapper) {
        LimsTrainEffectBean limsTrainEffect = jsonWrapper.parseUnique(LimsTrainEffectBean.class);
        limsTrainEffect.setId(ApplicationContextHelper.getNextIdentity());
        getDao().insert(limsTrainEffect);
        return limsTrainEffect.getId();
    }

    @Override
    public void finish(RestJsonWrapperBean wrapper) {
        List<LimsTrainEffectBean> clientBeans = wrapper.parse(LimsTrainEffectBean.class);
        clientBeans.forEach(e -> e.setStatus("finish"));
        getDao().update(clientBeans, "status");
    }
}
